<?php
// +—————————————————————————————————————————————————————————————————————
// | Created by Yunbao
// +—————————————————————————————————————————————————————————————————————
// | Copyright (c) 2013~2022 http://www.yunbaokj.com All rights reserved.
// +—————————————————————————————————————————————————————————————————————
// | Author: https://gitee.com/yunbaokeji
// +—————————————————————————————————————————————————————————————————————
// | Date: 2022-04-30
// +—————————————————————————————————————————————————————————————————————

class Model_Login extends PhalApi_Model_NotORM {
	
	protected $fields='id,user_nicename,avatar,avatar_thumb,sex,signature,coin,user_status,login_type,province,city,area,birthday,last_login_time,age,mobile';

	/* 会员登录 */   	
    public function userLogin($user_login,$source,$mobileid) {

    	$info1=DI()->notorm->user
				->select($this->fields)
				->where('user_login=? and user_type="1"',$user_login) 
				->fetchOne();
		if($info1){
			return 1004;
		}
		
		$info=DI()->notorm->user
				->select($this->fields)
				->where('user_login=? and user_type="2"',$user_login) 
				->fetchOne();

		$now=time();
		$nowYear=date("Y",$now);

		if(!$info){



			$from_ip= ip2long($_SERVER["REMOTE_ADDR"]);
			//判断是否超过后台的配置数
			$checklimit=checkRegIpLimit($mobileid,$from_ip);
			
			if($checklimit){
				return 1001;
			}


			$birthdayYear=2000;
			
			//新注册该用户
			$user_pass='qwe123';
			$user_pass=setPass($user_pass);
			$user_login=$user_login;

			$nickname='手机用户'.substr($user_login,-4);
			
			$avatar='/default.png';
			$avatar_thumb='/default_thumb.png';

			
			$data=array(
				'user_login' => $user_login,
				'user_nicename' =>$nickname,
				'user_pass' =>$user_pass,
				'signature' =>'这家伙很懒，什么都没留下',
				'avatar' =>$avatar,
				'avatar_thumb' =>$avatar_thumb,
				'bg_img' =>$avatar,
				'last_login_ip' =>$_SERVER['REMOTE_ADDR'],
				'create_time' => time(),
				'user_status' => 1,
				"user_type"=>2,//会员


				"age"=>$nowYear-$birthdayYear,
				"birthday"=>'2000-01-01',
				"mobile"=>$user_login,
				"login_type"=>'phone',
				"ip"=>$from_ip,
				"mobileid"=>$mobileid,

			);
            if($source){
                $data['source']=$source;
            }
			
			$rs=DI()->notorm->user->insert($data);	
			

		
			$info['id']=$rs['id'];
			$info['user_nicename']=$data['user_nicename'];
			$info['avatar']=get_upload_path($data['avatar']);
			$info['avatar_thumb']=get_upload_path($data['avatar_thumb']);
			$info['sex']='2';
			$info['signature']=$data['signature'];
			$info['coin']=$reg_reward;
			$info['login_type']=$data['login_type'];
			$info['province']='';
			$info['city']='';
			$info['birthday']='';
			$info['last_login_time']='';

			$info['age']="0";
			$info['mobile']=$user_login;
			$info['isreg']='1'; //此参数结合后台配置参数agent_must（邀请码是否必填）,如果邀请码非必填时，只有在此参数=1时app端才会弹窗显示邀请码
			$info['hometown']='';
			
		}else{

			//重新计算用户的年龄
			$month=date("m",strtotime($info['birthday']));
			$nowMonth=date("m",$now);
			if($nowMonth>=$month){
				$cha=0;
			}else{
				$cha=1;
			}

			$birthdayYear=date("Y",strtotime($info['birthday']));
			$age=$nowYear-$birthdayYear-$cha;

			DI()->notorm->user->where("id=?",$info['id'])->update(array("age"=>$age));

			if($info['user_status']=='0'){
				return 1002;					
			}

			if($info['user_status']=='3'){
				return 1003;					
			}

			unset($info['user_status']);
			
		
			
			$info['avatar']=get_upload_path($info['avatar']);
			$info['avatar_thumb']=get_upload_path($info['avatar_thumb']);
			$info['isreg']='0'; //此参数结合后台配置参数agent_must（邀请码是否必填）,如果邀请码非必填时，只有在此参数=1时app端才会弹窗显示邀请码
			$info['hometown']=$info['province'].$info['city'].$info['area'];

		}

			$token=md5(md5($info['id'].$user_login.time()));
			$info['token']=$token;
			$this->updateToken($info['id'],$token);

			$cache=array("token_".$info['id'],"userinfo_".$info['id']);
			delcache($cache);

		
        return $info;
    }	
	
	
	/* 更新token 登陆信息 */
    public function updateToken($uid,$token) {
		$expiretime=time()+60*60*24*300;
		$nowtime=time();
		
        DI()->notorm->user
			->where('id=?',$uid)
            ->update(array('last_login_time' => time(), "last_login_ip"=>$_SERVER['REMOTE_ADDR'] ));

        $isok=DI()->notorm->user_token
			->where('user_id=?',$uid)
			->update(array("token"=>$token, "expire_time"=>$expiretime ,'create_time' => $nowtime ));

		if(!$isok){
            DI()->notorm->user_token
			->insert(array("user_id"=>$uid,"token"=>$token, "expire_time"=>$expiretime ,'create_time' => $nowtime ));
        }

        $token_info=array(
        	'uid'=>$uid,
			'token'=>$token,
			'expire_time'=>$expiretime,
        );

        setcaches("token_".$uid,$token_info);

		return 1;
    }	
	




}
